TWB.New_Module({
	///////////////////////////
	name : "AttackOptions",  //
	author : "enduo",        //
	version : 1.0,           //
	///////////////////////////

	settings : {
		su_tutorial : {
			type : "checkbox"
		}
	},	
	GrabAID : function(){
		if(typeof TWB.AID=="undefined"){
			if(TWB.Modules.Icons.Screen()=="place"){
				var i;
				var flag=false;
				var V=TWB.gei("TWB-Villages").childNodes;
				for(i=0; i<V.length; i++){
					if(V[i].getAttribute('id').split("TWB-v")[1]!=TWB.Modules.BackNext.GetVil()){
						var target=V[i].getAttribute('tooltiptext').match(/(\d+)\|(\d+)/);
						break;
					}
				}
				var UN=TWB.Xpath("//a[contains(@href,'insertUnit')]",6);
				for(i=0; i<UN.length; i++){
					if(UN[i].textContent.match(/\d+/)[0]>0){
						flag=true;
						break;
					}
				}
				if(flag){
					var WN=TWB._window();
					if(typeof WN.wrappedJSObject!="undefined"){
						WN=WN.wrappedJSObject;
					}
					WN.selectAllUnits(true);
					TWB.Xpath("//*[@id='inputx']",9).value=target[1];
					TWB.Xpath("//*[@id='inputy']",9).value=target[2];
					// Launch process listener
					TWB.Processor.start("grab_aid","Modules.AttackOptions.GrabAID_","*",false);
					TWB.Xpath("//*[@type='submit']",9).click();
				}
				else{
					alert(TWB.lang("need_units"));
				}
			}
			else{
				TWB.Processor.start("router","Modules.AttackOptions.GrabAID","*",true);
				TWB.Modules.Icons.GoTo("place","command");
			}
		}
		else{
			// Post on forum
			if(TWB.Modules.Icons.Screen()=="forum"){
				var O=$xp("//textarea",9,TWB._window().document);
				if(O){
					O.value+="\n"+TWB.lang("aid")+": "+TWB.AID;
				}
				delete TWB.AID;
				TWB.gei("TWB-AttackOptions-GrabAID").style.color="";
			}
			else{
				// Need to be on forum
				alert(TWB.lang("notonforum"));
			}
		}
	},
	GrabAID_ : function(){
		if(TWB.Modules.Icons.Screen()=="place" && TWB.URL().match(/try=confirm/)==null){
			var i;
			TWB.AID=0;
			var T=TWB.Xpath("//a[contains(@href,'action=cancel')]",6);
			var last=null;
			for(i=0; i<T.length; i++){
				var t=T[i].href.match(/id=(\d+)/)[1];
				TWB.AID=TWB.compare(t,TWB.AID,1);
				if(TWB.AID==t){
					last=i;
				}
			}
			TWB.Load_Url(T[last].href);
			// Highlight the Attack options to make it ready
			TWB.gei("TWB-AttackOptions-GrabAID").style.color="blue";
			TWB.Processor.stop("grab_aid");
		}
	},
	SupportU : function(id){
		if(typeof TWB.Servers[TWB.language]=="undefined"){
			return alert(TWB.lang("servernotsup"));
		}
		if(typeof id=="undefined"){
			TWB.Modules.Villages.Action="Modules.AttackOptions.SupportU";
			if(TWB.get_var("su_tutorial")){
				TWB.Notify(TWB.lang("selector"),"TWB-AttackOptions");
			}
		}
		else{
			TWB.Modules.AttackOptions.tmp_id=id;
			TWB.Modules.Villages.Action="Modules.AttackOptions.SupportU_";
			if(TWB.get_var("su_tutorial")){
				TWB.Notify(TWB.lang("selectta"),"TWB-AttackOptions");
			}
		}
	},
	SupportU_ : function(to){
		var from=TWB.Modules.AttackOptions.tmp_id;
		delete TWB.Modules.AttackOptions.tmp_id;
		TWB.Load_Url(TWB.Modules.Villages.MakeUrl(from,"place","command",true)+"&target="+to);
	},
	BringSupports : function(){
		if(typeof TWB.Servers[TWB.language]!="undefined"){
			var range=prompt(TWB.lang("bringsups"));
			if(range!=null){
				var i;
				var V=TWB.gei("TWB-Villages").childNodes;
				if(range=="-1"){
					range="1-"+V.length;
				}
				range=range.match(/\d+\-\d+/);
				if(range){
					var cplayer=prompt(TWB.lang("bringsupsp"));
					range=range[0].split("-");
					var start=(range[0]<0)?0:range[0]-1;
					var endx=(range[1]>V.length)?V.length:range[1];
					TWB.bring_supports=[];
					if(cplayer!=null){
						TWB.bring_supports_p=cplayer.split(" ");
					}
					for(i=start; i<endx; i++){
						TWB.bring_supports.push(V[i].getAttribute("id").split("TWB-v")[1]);
					}
					TWB.Processor.start("bring_supports","Modules.AttackOptions.BringSupports_","*",false);
					TWB.Modules.Villages.MakeUrl(TWB.bring_supports[0],"place","units");
				}
				else{
					// Rerun
					TWB.Modules.AttackOptions.BringSupports();
				}
			}
		}
		else{
			alert(TWB.lang("servernotsup"));
		}
	},
	BringSupports_ : function(){
		var j;
		var links=TWB.Xpath("//a[contains(@href,'action=all_back')]",6);
		for(j=0; j<links.length; j++){
			var player=links[j].parentNode.parentNode.getElementsByTagName("td")[1].textContent.match(/\((.+)\)\s\(/)[1];
			if(typeof TWB.bring_supports_p=="undefined" || TWB.bring_supports_p.indexOf(player)>-1){
				TWB.$.ajax({url:links[j].href,async: false});
			}
		}
		// Move to next village
		TWB.bring_supports.splice(0,1);
		if(TWB.bring_supports.length>0){
			TWB.Modules.Villages.MakeUrl(TWB.bring_supports[0],"place","units");
		}
		else{
			delete TWB.bring_supports;
			delete TWB.bring_supports_p;
			TWB.Processor.stop("bring_supports");
		}
	}
	
});